package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class MotorOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int CutCard(int motorNumber)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] mBuffer = { -91, (byte)motorNumber, 17, 1, 0, 0, 0, 0, 0, 0, -38 };
      
      byte mXor = calculateXORSum(mBuffer, 0, 11);
      mBuffer = new byte[] { -91, (byte)motorNumber, 17, 1, 0, 0, 0, 0, 0, 0, -38, mXor };
      
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(mBuffer, mBuffer.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(mBuffer);
      }
      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 100, 2);
      if (ConfirmDataLength < 2) {
        return -1;
      }
      LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if (ConfirmData[0] != -121) {
        return -4;
      }
      byte[] ReConfirmData = { -98, (byte)motorNumber };
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
      serialPort.writeBytes(ReConfirmData);
      byte[] ReturnData = new byte[512];
      int ReturnDataLength = serialPort.readBytes(ReturnData, 700, 12);
      if (ReturnDataLength > 0)
      {
        byte[] Result = new byte[ReturnDataLength];
        System.arraycopy(ReturnData, 0, Result, 0, ReturnDataLength);
        LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(Result, Result.length));
        if (Result[2] != 85)
        {
          if (Result[9] == 5)
          {
            LogUtil.writeMotorOperateLog("机头故障:-2");
            return -2;
          }
          if (Result[9] == 3)
          {
            LogUtil.writeMotorOperateLog("机头故障:-3");
            return -3;
          }
          return -5;
        }
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("CutCard:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int QueryStatus(int motorNumber)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] mBuffer = { -91, (byte)motorNumber, 18, 0, 0, 0, 0, 0, 0, 0, -38 };
      
      byte mXor = calculateXORSum(mBuffer, 0, 11);
      mBuffer = new byte[] { -91, (byte)motorNumber, 18, 0, 0, 0, 0, 0, 0, 0, -38, mXor };
      
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(mBuffer, mBuffer.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(mBuffer);
      }
      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 100, 2);
      if (ConfirmDataLength < 2) {
        return -2;
      }
      LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if (ConfirmData[0] != -121) {
        return -1;
      }
      byte[] ReConfirmData = { -98, (byte)motorNumber };
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
      serialPort.writeBytes(ReConfirmData);
      byte[] ReturnData = new byte[512];
      int ReturnDataLength = serialPort.readBytes(ReturnData, 200, 12);
      if (ReturnDataLength > 0)
      {
        byte[] Result = new byte[ReturnDataLength];
        System.arraycopy(ReturnData, 0, Result, 0, ReturnDataLength);
        LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(Result, Result.length));
        if (Result[2] == 85)
        {
          int mlotteryNum1 = Result[9] & 0x3;
          if (mlotteryNum1 > 0)
          {
            LogUtil.writeMotorOperateLog("机头无票");
            



            return -5;
          }
          int mlotteryNum2 = Result[9] & 0x20;
          if (mlotteryNum2 > 0) {
            return -7;
          }
          return 1;
        }
        if (Result[6] == 6) {
          return -8;
        }
        LogUtil.writeMotorOperateLog("机头未知故障");
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("QueryStatus:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SetCard(int motorNumber, int cardLength)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] mCardSize = new byte[2];
      if (cardLength > 102)
      {
        cardLength -= 128;
        mCardSize[0] = 1;
        mCardSize[1] = ((byte)cardLength);
      }
      else
      {
        mCardSize[0] = 0;
        mCardSize[1] = ((byte)cardLength);
      }
      byte[] mBuffer = { -91, (byte)motorNumber, 16, 0, 0, 0, 0, 0, mCardSize[0], mCardSize[1], -38 };
      
      byte mXor = calculateXORSum(mBuffer, 0, 11);
      mBuffer = new byte[] { -91, (byte)motorNumber, 16, 0, 0, 0, 0, 0, mCardSize[0], mCardSize[1], -38, mXor };
      
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(mBuffer, mBuffer.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(mBuffer);
      }
      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 100, 2);
      if (ConfirmDataLength < 2) {
        return -2;
      }
      LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if (ConfirmData[0] != -121) {
        return -1;
      }
      byte[] ReConfirmData = { -98, (byte)motorNumber };
      LogUtil.writeMotorOperateLog("write:" + LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
      serialPort.writeBytes(ReConfirmData);
      byte[] ReturnData = new byte[512];
      int ReturnDataLength = serialPort.readBytes(ReturnData, 200, 12);
      if (ReturnDataLength > 0)
      {
        byte[] Result = new byte[ReturnDataLength];
        System.arraycopy(ReturnData, 0, Result, 0, ReturnDataLength);
        LogUtil.writeMotorOperateLog("read:" + LogUtil.bytesToHexString(Result, Result.length));
        if (Result[2] == 85)
        {
          int mlotteryNum1 = Result[9] & 0x3;
          if (mlotteryNum1 > 0) {
            return -5;
          }
          int mlotteryNum2 = Result[9] & 0x20;
          if (mlotteryNum2 > 0) {
            return -7;
          }
          return 1;
        }
        if (Result[6] == 6) {
          return -8;
        }
        LogUtil.writeMotorOperateLog("机头未知故障");
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("SetCard:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte calculateXORSum(byte[] b, int startIndex, int len)
  {
    byte xor = 0;
    for (int i = 0; i < len; i++) {
      xor = (byte)(xor ^ b[(startIndex + i)]);
    }
    return xor;
  }
}
